﻿
function openaccountant(evt, acname) {
    var i, x, tablinks;
    x = document.getElementsByClassName("acc");
    for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("accountantlink");
    for (i = 0; i < x.length; i++) {
        tablinks[i].className = tablinks[i].className.replace(" w3-teal w3-topbar w3-border-white", " w3-text-teal");
    }
    document.getElementById(acname).style.display = "block";
    evt.currentTarget.className = evt.currentTarget.className.replace(" w3-text-teal", " w3-teal w3-topbar w3-border-white");
}

$(document).ready(function () {
    //$("#btn_search").on("click", function () {
    //    var month = document.getElementById("ddl_select_month").value;
    //    var year = document.getElementById("ddl_select_year").value;
    //    var staff = document.getElementById("ddl_staff").value;
    //    document.getElementById('check_area').innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
    //    var xhttp;
    //    xhttp = new XMLHttpRequest();
    //    xhttp.onreadystatechange = function () {
    //        if (xhttp.readyState == 4 || xhttp.status == 200) {
    //            document.getElementById('check_area').innerHTML = xhttp.responseText;
    //        }
    //    };
    //    xhttp.open("get", "../ajax/asp/accountant/check_by_month.aspx?date_month=" + month + "&date_year=" + year + "&staff=" + staff, true);
    //    xhttp.send();
    //});
    $("#btn_search_month").on("click", function () {
        var month = document.getElementById("ddl_month").value;
        var year = document.getElementById("ddl_year").value;
        document.getElementById('report_time_staff').innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById('report_time_staff').innerHTML = xhttp.responseText;
            }
        };
        xhttp.open("GET", "../ajax/asp/accountant/report_time_staff.aspx?month=" + month + "&year=" + year, true);
        xhttp.send();
    });
    $("#filteroption").on("change", function () {
        if (this.value == 0) {
            document.getElementById('filterday').style.display = "none";
            document.getElementById('filtermonth').style.display = "inline-block";
            document.getElementById('filteryear').style.display = "inline-block";
        }
        else {
            document.getElementById('filterday').style.display = "inline-block";
            document.getElementById('filtermonth').style.display = "none";
            document.getElementById('filteryear').style.display = "none";
        }
    });
    $("#filter-option").on("change", function () {
        if (this.value == 0) {
            document.getElementById('bill_month').style.display = "inline-block";
            document.getElementById('bill_year').style.display = "inline-block";
            document.getElementById("filter-class").style.display = "inline-block";
            document.getElementById('find_member').style.display = "none";
        }
        else {
            document.getElementById('bill_month').style.display = "none";
            document.getElementById('bill_year').style.display = "none";
            document.getElementById("filter-class").style.display = "none";
            document.getElementById('find_member').style.display = "inline-block";
        }
    });
    $(".study-pay-btn").click(function () {
        document.getElementById("study-pay-modal").style.display = 'block';
    });
    $(".save-pay-btn").click(function () {
        document.getElementById("save-pay-modal").style.display = 'block';
    });
    $(".bill").click(function () {
        var id = this.getElementsByClassName("bill_id")[0].innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                $('#edit-study-pay-modal').fadeIn();
                document.getElementById('edit_bill_area').innerHTML = xhttp.responseText;
                $("#btn_edit_bill").click(function () {
                    var id = document.getElementById("edit_bill_id").innerHTML;
                    var fee = document.getElementById("edit_fee").value;
                    var packet = document.getElementById("edit_packet").value;
                    var exp = document.getElementById("edit_date").value;
                    if (fee == "") {
                        document.getElementById("edit_error_msg").innerHTML = "Học phí không được để trống";
                    }
                    else {
                        if (exp == "") {
                            document.getElementById("edit_error_msg").innerHTML = "Ngày hết hạn sai định dạng";
                        }
                        else {
                            var edit;
                            edit = new XMLHttpRequest();
                            edit.onreadystatechange = function () {
                                if (edit.readyState == 4 && edit.status == 200) {
                                    location.reload(true);
                                }
                            };
                            edit.open("GET", "../ajax/asp/accountant/edit_bill.aspx?id=" + id + "&fee=" + fee + "&packet=" + packet + "&exp=" + exp, true);
                            edit.send();
                        }
                    }
                });
                $("#btn_accept_del_bill").click(function () {
                    document.getElementById('delete-study-pay-modal').style.display = 'block';
                    document.getElementById("del_bill_id").innerHTML = document.getElementById("edit_bill_id").innerHTML;
                });
            }
        };
        xhttp.open("GET", "../ajax/asp/accountant/edit_bill_form.aspx?id=" + id, true);
        xhttp.send();

    });
    $("#btn_study_search").click(function () {
        var bill_style = document.getElementById("filter-option").value;
        if (bill_style == 0) {
            var op_class = document.getElementById("filter-class").value;
            var month = document.getElementById("bill_month").value;
            var year = document.getElementById("bill_year").value;
            document.getElementById('check_bill_area').innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    document.getElementById('check_bill_area').innerHTML = xhttp.responseText;
                    $(".bill").click(function () {
                        var id = this.getElementsByClassName("bill_id")[0].innerHTML;
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                $('#edit-study-pay-modal').fadeIn();
                                document.getElementById('edit_bill_area').innerHTML = xhttp.responseText;
                                $("#btn_edit_bill").click(function () {
                                    var id = document.getElementById("edit_bill_id").innerHTML;
                                    var fee = document.getElementById("edit_fee").value;
                                    var packet = document.getElementById("edit_packet").value;
                                    var exp = document.getElementById("edit_date").value;
                                    if (fee == "") {
                                        document.getElementById("edit_error_msg").innerHTML = "Học phí không được để trống";
                                    }
                                    else {
                                        if (exp == "") {
                                            document.getElementById("edit_error_msg").innerHTML = "Ngày hết hạn sai định dạng";
                                        }
                                        else {
                                            var edit;
                                            edit = new XMLHttpRequest();
                                            edit.onreadystatechange = function () {
                                                if (edit.readyState == 4 && edit.status == 200) {
                                                    location.reload(true);
                                                }
                                            };
                                            edit.open("GET", "../ajax/asp/accountant/edit_bill.aspx?id=" + id + "&fee=" + fee + "&packet=" + packet + "&exp=" + exp, true);
                                            edit.send();
                                        }
                                    }
                                });
                                $("#btn_accept_del_bill").click(function () {
                                    document.getElementById('delete-study-pay-modal').style.display = 'block';
                                    document.getElementById("del_bill_id").innerHTML = document.getElementById("edit_bill_id").innerHTML;
                                });
                            }
                        };
                        xhttp.open("GET", "../ajax/asp/accountant/edit_bill_form.aspx?id=" + id, true);
                        xhttp.send();
                        
                    });
                }
            };
            xhttp.open("GET", "../ajax/asp/accountant/find_Bill_By_Month.aspx?class_id="+op_class+"&month=" + month + "&year=" + year, true);
            xhttp.send();
        }
        else {
            var study = document.getElementById("find_member").value;
            document.getElementById('check_bill_area').innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    document.getElementById('check_bill_area').innerHTML = xhttp.responseText;
                    $(".bill").click(function () {
                        var id = this.getElementsByClassName("bill_id")[0].innerHTML;
                        var xhttp;
                        xhttp = new XMLHttpRequest();
                        xhttp.onreadystatechange = function () {
                            if (xhttp.readyState == 4 && xhttp.status == 200) {
                                $('#edit-study-pay-modal').fadeIn();
                                document.getElementById('edit_bill_area').innerHTML = xhttp.responseText;
                                $("#btn_edit_bill").click(function () {
                                    var id = document.getElementById("edit_bill_id").innerHTML;
                                    var fee = document.getElementById("edit_fee").value;
                                    var packet = document.getElementById("edit_packet").value;
                                    var exp = document.getElementById("edit_date").value;
                                    if (fee == "") {
                                        document.getElementById("edit_error_msg").innerHTML = "Học phí không được để trống";
                                    }
                                    else {
                                        if (exp == "") {
                                            document.getElementById("edit_error_msg").innerHTML = "Ngày hết hạn sai định dạng";
                                        }
                                        else {
                                            var edit;
                                            edit = new XMLHttpRequest();
                                            edit.onreadystatechange = function () {
                                                if (edit.readyState == 4 && edit.status == 200) {
                                                    location.reload(true);
                                                }
                                            };
                                            edit.open("GET", "../ajax/asp/accountant/edit_bill.aspx?id=" + id + "&fee=" + fee + "&packet=" + packet + "&exp=" + exp, true);
                                            edit.send();
                                        }
                                    }
                                });
                                $("#btn_accept_del_bill").click(function () {
                                    document.getElementById('delete-study-pay-modal').style.display = 'block';
                                    document.getElementById("del_bill_id").innerHTML = document.getElementById("edit_bill_id").innerHTML;
                                });
                            }
                        };
                        xhttp.open("GET", "../ajax/asp/accountant/edit_bill_form.aspx?id=" + id, true);
                        xhttp.send();
                    });
                }
            };
            xhttp.open("GET", "../ajax/asp/accountant/find_Bill_By_Member.aspx?member=" + study.trim(), true);
            xhttp.send();
        }
    });

    $(".check_number").click(function () {
        var study_number = document.getElementById("study_number").value;
        if (study_number.trim() == "")
            document.getElementById("student-detail").innerHTML = "<div class='w3-row w3-center w3-text-red'>Mã học viên không được để trống</div>"
        else {
            document.getElementById('student-detail').innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    document.getElementById('student-detail').innerHTML = xhttp.responseText;
                    $("#btn_bill_accept").click(function () {
                        var id = document.getElementById("student_id").innerHTML;
                        var fee = document.getElementById("detail_fee").value;
                        var packet = document.getElementById("ddl_detail_month").value;
                        var exp = document.getElementById("detail_exp_date").value;
                        var error = document.getElementById("error_bill_msg");
                        if (fee == "") {
                            error.innerHTML = "Học phí không được để trống";
                        } else {
                            if (exp == "") {
                                error.innerHTML = "Ngày hết hạn sai định dạng";
                            }
                            else {
                                var xhttp;
                                xhttp = new XMLHttpRequest();
                                xhttp.onreadystatechange = function () {
                                    if (xhttp.readyState == 4 && xhttp.status == 200) {
                                        location.reload(true);
                                    }
                                };
                                xhttp.open("GET", "../ajax/asp/accountant/add_bill.aspx?id=" + id + "&fee=" + fee + "&packet=" + packet + "&exp=" + exp, true);
                                xhttp.send();
                            }
                        }
                    });
                }
            };
            xhttp.open("GET", "../ajax/asp/accountant/check_number_study.aspx?study_number=" + study_number.trim()+"&type=1", true);
            xhttp.send();
        }
    });

    $("#btn_Save_Fee").click(function () {
        var study_number = document.getElementById("study_number2").value;
        if (study_number.trim() == "")
            document.getElementById("student-detail2").innerHTML = "<div class='w3-row w3-center w3-text-red'>Mã học viên không được để trống</div>"
        else {
            document.getElementById('student-detail2').innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
            var xhttp;
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    document.getElementById('student-detail2').innerHTML = xhttp.responseText;
                }
            };
            xhttp.open("GET", "../ajax/asp/accountant/check_number_study.aspx?study_number=" + study_number.trim() +"&type=2", true);
            xhttp.send();
        }
    });

    $("#btn_del_bill").click(function () {
        var id = document.getElementById("del_bill_id").innerHTML;
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                $("#bill" + id).remove();
                document.getElementById('delete-study-pay-modal').style.display = 'none';
                document.getElementById('edit-study-pay-modal').style.display = 'none';
            }
        };
        xhttp.open("GET", "../ajax/asp/accountant/del_bill.aspx?id=" + id, true);
        xhttp.send();
    });

    $("#filter_class").on("change", function () {
        var id = document.getElementById("filter_class").value;
        document.getElementById('study_fee_area').innerHTML = '<div id="loading"><div id="loading-center-absolute"><div class="object w3-text-teal" id="object_four"></div><div class="object w3-text-teal" id="object_three"></div><div class="object w3-text-teal" id="object_two"></div><div class="object w3-text-teal" id="object_one"></div></div></div>';
        var xhttp;
        xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("study_fee_area").innerHTML = xhttp.responseText;
            }
        };
        xhttp.open("GET", "../ajax/asp/accountant/filter_Class_Fee.aspx?id=" + id, true);
        xhttp.send();
    });
});
function btn_accept_save_fee(id) {
    var sf_date = document.getElementById("sf_date").value;
    var sf_exp_day = document.getElementById("sf_exp_day").value;
    if (sf_date == "") {
        document.getElementById("save_Fee_msg").innerHTML = "Ngày bảo lưu sai định dạng";
    }
    else {
        if (sf_exp_date == "") {
            document.getElementById("save_Fee_msg").innerHTML = "Số tiền còn lại không được để trống";
        }
        else {
            $.post("../Ajax/ASP/Accountant/accept_Save_Fees.aspx", { "id": id, "exp_date": sf_exp_day, "date": sf_date });
            location.reload(true);
        }
    }
}
function btn_accept_open_save_fee(id) {
    var cls = document.getElementById("sf_choose_class").value;
    var sf_open_date = document.getElementById("sf_open_date").value;
    if (cls == -1) {
        document.getElementById("save_Fee_msg").innerHTML = "Hãy chọn lớp cho học viên này";
    }
    else {
        if (sf_open_date == "") {
            document.getElementById("save_Fee_msg").innerHTML = "Ngày mở lớp sai định dạng";
        }
        else {
            $.post("../Ajax/ASP/Accountant/accept_Open_Save_Fees.aspx", { "id": id, "cls": cls, "date": sf_open_date });
            location.reload(true);
        }
    }
}